#include "m24128.h"
#include "i2cmaster.h"


uint8_t eeppromWriteByte(uint16_t addr, uint8_t  data){
    uint8_t ret = 0;
    ret = i2c_start(EEPROM_ADDR+I2C_WRITE);
    i2c_check(ret);

    ret += i2c_write(highByte(addr));
    ret += i2c_write(lowByte(addr));
    ret += i2c_write(data);

    i2c_stop();
    return ret;
}


uint8_t eepromWriteSeq  (uint16_t addr, uint8_t* data, uint8_t length){
    uint8_t i = 0;
    uint8_t ret = 0;
    ret = i2c_start(EEPROM_ADDR+I2C_WRITE);
    i2c_check(ret);

    ret += i2c_write(highByte(addr));
    ret += i2c_write(lowByte(addr));

    for(i=0;i<length;i++){
        ret += i2c_write(data);
    }

    i2c_stop();
    return ret;
}


uint8_t eepromReadByte(uint16_t addr, uint8_t* data){
    uint8_t ret = 0;
    ret = i2c_start(EEPROM_ADDR+I2C_WRITE);
    i2c_check(ret);

    ret += i2c_write(highByte(addr));
    ret += i2c_write(lowByte(addr));
    i2c_check(ret);

    ret = i2c_rep_start(EEPROM_ADDR+I2C_READ);
    i2c_check(ret);

    ret = i2c_read(NACK);

    i2c_stop();
    return ret;
}

uint8_t eepromReadSeq(uint16_t addr, uint8_t* data, uint8_t length){
    uint8_t ret = 0;
    uint8_t i = 0;
    ret = i2c_start(EEPROM_ADDR+I2C_WRITE);
    i2c_check(ret);

    ret += i2c_write(highByte(addr));
    ret += i2c_write(lowByte(addr));
    i2c_check(ret);

    ret = i2c_rep_start(EEPROM_ADDR+I2C_READ);
    i2c_check(ret);

    for(i=0;i<length-1;i++){
        data[i] = i2c_read(ACK);
    }
    data[i] = i2c_read(NACK);

    i2c_stop();
    return ret;
}